﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using Sharp.DataTransferObject;
using Sharp.Infrastructure.Communication;
using Sharp.Platform.Mvc;
using Sharp.ServiceContracts;
using Sharp.Web.Models;
using PartialViewResult = Ext.Net.MVC.PartialViewResult;

namespace Sharp.Web.Controllers
{
    [AutomaticTemplate]
    public class OrgController : Controller
    {
        // GET: Manage/Org
        public ActionResult Index(string containerId)
        {
            return new PartialViewResult
            {
                ViewName = "Index",
                Model = new OrgIndexModel(),
                ContainerId = containerId,
                RenderMode = RenderMode.AddTo
            };
        }

        public StoreResult GetTreeData(string node)
        {
            var LeftTree = new Node()
            {
                Text = "根节点",
                NodeID = "Root",
                Expanded = true,
                //AutoDataBind = false,
                Leaf = true,
                Checked = null
            };
            IEnumerable<OrgDTO> list;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                list = proxy.Channel.GetOrgs();
            }
            TreeBuilder<OrgDTO> builder = new TreeBuilder<OrgDTO>
            {
                ChangeData = (p, treeNode) =>
                {
                    treeNode.Text = p.Title;
                    treeNode.NodeID = p.Id.ToString();
                    treeNode.AllowDrag = true;
                    treeNode.AllowDrop = true;
                    treeNode.AttributesObject = p;
                    treeNode.Leaf = list.All(x => x.ParentId != p.Id);

                    treeNode.Expanded = true;
                }
            };

            builder.Build(list, LeftTree, null);

            return this.Store(LeftTree.Children, list.Count());
        }

        public ActionResult MoveOrSort(MoveOrSortInfo info)
        {
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.OrgMoveOrSort(info.ParentId, info.Ids);

                return Content(res.ToJson());
            }
        }

        public ActionResult Details(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrgDTO dto = null;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetOrgInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }
            var vm = dto.To<OrgModel>();
            vm.OrgTree = OrgTree();

            return new PartialViewResult { Model = vm };
        }
        // GET: Manage/Org/Create
        public ActionResult Create()
        {
            var vm = new OrgModel();
            vm.OrgTree = OrgTree();
            return new PartialViewResult { Model = vm };
        }
        // GET: Manage/User/Edit/5
        public ActionResult Edit(Guid? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrgDTO dto = null;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetOrgInfo(id.Value);
            }
            if (dto == null)
            {
                return HttpNotFound();
            }
            var vm = dto.To<OrgModel>();
            vm.OrgTree = OrgTree();

            return new PartialViewResult { Model = vm };
        }
        // POST: Manage/Org/Create
        [HttpPost]
        public ActionResult Save(OrgModel org)
        {
            OrgDTO dto = org.To<OrgDTO>();

            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                proxy.Channel.SaveOrg(dto);
            }

            return this.FormPanel();
        }
 
 
        // POST: Manage/Org/Delete/5
        [HttpPost]
        public ActionResult Delete(Guid[] ids)
        {
            var res = new JsonResult();
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                res.Data = proxy.Channel.DeleteOrgs(ids);
            }

            return res;
        }

        public static Node OrgTree()
        {
            var leftTree = new Node()
            {
                Text = ".",
                NodeID = "Root",
                Expanded = true,
                AutoDataBind = false,
                Checked = null
            };
            IEnumerable<OrgDTO> list;
            using (var proxy = new ServiceProxy<IEnterpriseService>())
            {
                list = proxy.Channel.GetOrgs();


            }
            var builder = new TreeBuilder<OrgDTO>
            {
                ChangeData = (p, treeNode) =>
                {
                    treeNode.Text = p.Title;
                    treeNode.NodeID = p.Id.ToString();
                    treeNode.AutoDataBind = false;
                    treeNode.Leaf = list.All(x => x.ParentId != p.Id);
                    treeNode.Expanded = true;
                }
            };

            builder.Build(list, leftTree, null);

            return leftTree;
        }
    }
}
